package com.crk.yiyuanguahao.controller;

import com.crk.yiyuanguahao.entity.User;
import com.crk.yiyuanguahao.service.UserService;
import com.crk.yiyuanguahao.util.LayUiTableJson;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author
 * @description:
 * @date 2020/2/3
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("manage")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView("/workspace/user/userList.html");
        return modelAndView;
    }

    @GetMapping("detail")
    public ModelAndView detail(){
        ModelAndView modelAndView = new ModelAndView("/workspace/user/userAdd.html");
        return modelAndView;
    }

    /**
     * 保存用户
     * @param user 用户
     * @return
     */
    @PostMapping("/saveUser")
    public ResponseResult saveUser(@RequestBody User user){
        return userService.saveUser(user);
    }


    /**
     * 更改用户状态
     * @param userId 用户Id
     * @param status 用户状态
     * @return
     */
    @PostMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestParam Integer userId, @RequestParam Integer status){
        return userService.changeStatus(userId,status);
    }



    /**
     * 用户修改密码
     * @param
     * @return
     */
    @PostMapping("/changePassword")
    public ResponseResult changePassword(@RequestParam String oldPwd, @RequestParam String newPwd){
        return userService.changePassword(oldPwd,newPwd);
    }


    @PostMapping("/deleteUser")
    public ResponseResult deleteUser(@RequestParam("userId") Integer userId){
        return userService.deleteUser(userId);
    }



    /**
     * 查询用户列表 layui url只能用get方法
     * @param
     * @return
     */
    @RequestMapping("/getSysUserList")
    public LayUiTableJson getSysUserList(String userName, String keyword, @RequestParam(defaultValue = "1")String orderBy,
                                         @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer size){

        ResponseResult responseResult = userService.getSysUserList(userName,keyword, orderBy, currentPage, size);
        if (ResponseResult.isSuccess(responseResult)){
            return new LayUiTableJson(0,null,responseResult.getPage().getAllRow(),(List<User>)responseResult.getData());
        }else{
            return new LayUiTableJson(1,null,0,null);
        }
    }


    /**
     * 查询单个用户
     * @param userId
     * @return
     */
    @RequestMapping("/getSysUserById")
    public ResponseResult getSysUserById(@RequestParam("userId") Integer userId){
        return userService.getSysUserById(userId);
    }

    @PostMapping("/resetPwd")
    public ResponseResult resetPwd(@RequestParam("userId") Integer userId){
        return userService.resetPwd(userId);
    }


    @GetMapping("/permission")
    public ModelAndView permission(){
        ModelAndView modelAndView = new ModelAndView("/workspace/user/permission.html");
        return modelAndView;
    }

    @GetMapping("getUserList")
    public ResponseResult getUserList(){
        List<User> allUser = userService.getAllUser();
        return ResponseResult.success(allUser);
    }
}
